/**
 * Name: Avinash Sridhar	G Number: G00861300
 */
package udp;

import java.io.File;
import java.io.IOException;
import java.net.DatagramSocket;
import java.util.ArrayList;
import java.util.Scanner;

/**
 * @author asridhar
 *
 */
public class Server {

	public Server(String phonebookName, String clientPort, String childrenPort, String parentPort) throws IOException {
		//Load Phonebook
		//PhonebookLookupService = new LookupService(phonebookName);
		mPhonebook = new ArrayList<PhonebookEntry>();
		for (int i=0; i < 32; i++)	//fill with max 32 blanks...easier during file input
		{
			mPhonebook.add(new PhonebookEntry());
		}
		loadFromFile(phonebookName);

		//Initialize Client Socket Port - where you will be accepting requests from clients
		if (!clientPort.equals("NULL"))
			mClientSocketPort = new DatagramSocket(Integer.parseInt(clientPort));
		else
			mClientSocketPort = null;

		//Initialize Children Socket Port - where you will be accepting requests from children
		if (!childrenPort.equals("NULL"))
			mChildrenSocketPort = new DatagramSocket(Integer.parseInt(childrenPort));
		else
			mChildrenSocketPort = null;

		//Initialize Parent Socket Port - where this server will be sending requests
		if (!parentPort.equals("NULL"))
			mParentSocketPort = Integer.parseInt(parentPort);
		else
			mParentSocketPort = -1;

		//Initialize & Run ClientRequestHandler
		mClientRequestHandler = new ClientRequestHandler(mPhonebook, mClientSocketPort, mChildrenSocketPort, mParentSocketPort);
		mClientRequestHandler.start();

		//Initialize & Run ChildRequestHandler
		if (mChildrenSocketPort != null)
		{
			mChildRequestHandler = new ChildRequestHandler(mPhonebook, mClientSocketPort, mChildrenSocketPort, mParentSocketPort);
			mChildRequestHandler.start();
		}
	}

	public void loadFromFile(String filename) {
		try {
			Scanner liner = new Scanner(new File(filename)); 

			String line; 
			int lineIndex = 0; 
			while (liner.hasNextLine())
			{ 
				line = liner.nextLine();
				if (lineIndex % 4 == 0)
				{ 
					//System.out.println("Last name: " + line);
					mPhonebook.get(lineIndex/4).mLastName = line.trim();
				}
				else if ((lineIndex-1) % 4 == 0)
				{
					//System.out.println("First name: " + line);
					mPhonebook.get((lineIndex-1)/4).mFirstName = line.trim();
				}
				else if ((lineIndex-2) % 4 == 0)
				{
					//System.out.println("Phone: " + line);
					mPhonebook.get((lineIndex-2)/4).mPhoneNumber = line.trim();
				}

				lineIndex++;
			}
			liner.close();

		} catch (IOException e) {
			System.out.println("Phonebook not found! Please place <phonebook file> in " + System.getProperty("user.dir") + " . Thanks! Server exiting!");
			System.exit(0);
		} 
	}

	public class PhonebookEntry {
		public PhonebookEntry() {}
		public PhonebookEntry(String lastName, String firstName, String phoneNumber) {
			super();
			mLastName = lastName;
			mFirstName = firstName;
			mPhoneNumber = phoneNumber;
		}
		String mLastName;
		String mFirstName;
		String mPhoneNumber;
	}

	ArrayList<PhonebookEntry> mPhonebook;
	DatagramSocket mClientSocketPort;
	DatagramSocket mChildrenSocketPort;
	int mParentSocketPort;

	ClientRequestHandler mClientRequestHandler;
	ChildRequestHandler mChildRequestHandler;


	public static void main(String[] args) throws IOException {

		@SuppressWarnings({ "unused" })
		Server PhonebookServer = new Server (args[0], args[1], args[2], args[3]);
	}
}
